package com.bw.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.conf.Result;
import com.bw.domain.Onlineusers;
import com.bw.domain.SysUser;
import com.bw.service.OnlineUserService;
import com.bw.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    SysUserService service;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OnlineUserService onlineUserService;

    @RequestMapping("login")
    public Result login(@RequestBody SysUser user, HttpServletRequest request){

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUserName,user.getUserName());
        SysUser one = service.getOne(wrapper);
        if(one==null){
            return Result.error("用户名不存在");
        }
        String md5Hex = DigestUtil.md5Hex(user.getPassword());
        if(!md5Hex.equals(one.getPassword())){
            return Result.error("密码错误");
        }
        String uuid = request.getHeader("uuid");
        if(user.getCode()==null){
            return Result.error("验证码为空");
        }
        String redisCode =(String) redisTemplate.opsForValue().get("code_" + uuid)+"";
        System.out.println(redisCode);
        if(redisCode==null){
            return Result.error("验证码过期");
        }
        if(!user.getCode().equalsIgnoreCase(redisCode)){
            return Result.error("验证码错误");
        }
        Boolean login = redisTemplate.opsForValue().setIfAbsent("login", "lkk", 1, TimeUnit.MINUTES);
        if(login){

            Map<String,Object> map=new HashMap<>();
            map.put("user",one);
            String token = JWTUtil.createToken(map, "111".getBytes());
            map.put("token",token);
            onlineUserService.addOnlineUser(token,one);
            redisTemplate.opsForValue().set("user_"+uuid,uuid,5,TimeUnit.MINUTES);
            return Result.ok(map);
        }else{
            return Result.error("请勿重复登录");
        }
    }

    @RequestMapping("/list/{page}/{size}")
    public List<Onlineusers> list(@PathVariable int page, @PathVariable int size,@RequestBody Onlineusers onlineusers){
        return onlineUserService.lists(page, size,onlineusers);
    }

    @RequestMapping("qiangtui")
    public void qiangtuize(){
        redisTemplate.delete("login");
        redisTemplate.delete("username");
    }


    @RequestMapping("getCode")
    public Result getCode(String uuid){
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(160, 60, 4, 20);
        String code = captcha.getCode();
        Map<String,Object> map=new HashMap<>();
        redisTemplate.opsForValue().set("code_"+uuid,code,1, TimeUnit.MINUTES);
        map.put("img",captcha.getImageBase64Data());

        return Result.ok(map);
    }

    @RequestMapping("list")
    public Result list(){
        List<SysUser> list = service.list();
        return Result.ok(list);
    }

    @RequestMapping("add")
    public Result add(@RequestBody SysUser sysUser){
        boolean save = service.save(sysUser);
        return Result.ok(save);
    }

    @RequestMapping("deleteByid")
    public Result deleteByid(Integer id){
        boolean b = service.removeById(id);
        return Result.ok(b);
    }

    @RequestMapping("updateByid")
    public Result updateByid(@RequestBody SysUser user){
        boolean b = service.updateById(user);
        return Result.ok(b);
    }



}
